<?php

namespace Admin\Controllers;

class AdminController extends AuthController {

    public function indexAction() {
        $adminId = !empty($_POST['adminId']) ? $_POST['adminId'] : "";
        $remark = !empty($_POST['role']) ? trim($_POST['role']) : "";
        $realName = !empty($_POST['realName']) ? trim($_POST['realName']) : "";

        //获取所有角色
        $remarks = service("Admin/RoleService")->getAllRole();

        //获取查询的角色ID
        if (!empty($remark)) {
            foreach ($remarks as $r) {
                if ($r['remark'] == $remark) {
                    $remarkId = $r['id'];
                    $condition['roleId'] = $remarkId;
                }
            }
            //查询不到时直接不返回数据
            if (empty($remarkId)) {
                $this->view->allAdminUser = null;
                $this->view->realName = $realName;
                return;
            }
        }

        if ($adminId) {
            $condition['adminId'] = $adminId;
        }

        if ($realName) {
            $condition['realName'] = $realName;
        }

        $userModel = model("Admin/AdminUser");

        $count = $userModel->getCountNumber($condition);
        $list = $userModel->getLists($condition, $fieldArr = array(), $userModel->getPk() . " desc", $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1, $pagesize = 10, $groupStr = "");
        //分页类
        $Page = new \Platform\Libs\Page($count, PAGE_SIZE);
        //分页跳转的时候保证查询条件
        foreach ($_REQUEST as $key => $val) {
            if ($key != "_url" && $val != "") {
                $Page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }

        $show = $Page->show();
        $this->view->allAdminUser = $list;
        $this->view->realName = $realName;
        $this->view->show = $show;
    }

    /**
     * 添加管理角色页面跳转
     */
    public function addRoleAction() {
        
    }

    /**
     * 添加角色
     */
    public function addAction() {
        $name = !empty($_POST["name"]) ? trim($_POST["name"]) : E("管理员名称不能为空");
        $role = !empty($_POST["role"]) ? trim($_POST["role"]) : E("管理员角色不能为空");

        //判断是否为超级管理员
        if ($this->adminId != 1) {
            E("不是超级管理员，无法添加角色");
        }
        //添加角色
        $roleService = service("Admin/RoleService");
        $roleService->addRole($name, $role);
        $this->ajaxReturn("");
    }

    /**
     * 添加管理员页面跳转
     */
    public function addAdminAction() {
        //查询所有用户角色
        $roleService = service("Admin/RoleService");
        $allRole = $roleService->getAllRole();
        $this->view->allRole = $allRole;
    }

    /**
     * 添加管理员
     */
    public function addUserAction() {
        $username = !empty($_POST["username"]) ? trim($_POST["username"]) : E("用户名不能为空");
        $password = !empty(md5($_POST["password"])) ? md5(trim($_POST["password"])) : E("密码不能为空");
        $remark = !empty($_POST["role"]) ? trim($_POST["role"]) : E("管理员角色不能为空");
        $realName = !empty($_POST["realName"]) ? trim($_POST["realName"]) : E("管理员真实姓名不能为空");
        $img = !empty($_POST['img']) ? trim($_POST['img']) : E("管理员头像不能为空");
        $adminId = $this->adminId;
        if ($adminId != 1) {
            E("不是超级管理员，无法添加管理员");
        }
        //查询管理员是否存在
        $adminService = service("Admin/AdminUserService");
        $adminUser = $adminService->getAdminUserByUsername($username);
        if ($adminUser) {
            E("用户已存在");
        }
        //创建管理员
        $adminService->addAdminUser($username, $password, $remark, $realName, $img, $adminId);
        $this->ajaxReturn("");
    }

    /**
     * 修改用户信息页面跳转
     */
    public function updateUserInfoAction() {
        //获取当前登陆用户信息
        $adminId = $this->adminId;
        $adminService = service("Admin/AdminUserService");
        $adminUser = $adminService->getAdminUserById($adminId);
        $remarks = service("Admin/RoleService")->getAllRole();
        $this->view->adminUser = $adminUser;
        $this->view->remarks = $remarks;
    }

    /**
     * 修改用户信息
     */
    public function updateUserAction() {
        $password = !empty(trim($_POST['password'])) ? md5(trim($_POST['password'])) : "";
        $newPassword = !empty(trim($_POST['newPassword'])) ? md5(trim($_POST['newPassword'])) : "";
        $img = !empty(trim($_POST['img'])) ? trim($_POST['img']) : "";

        $adminService = service("Admin/AdminUserService");
        //获取当前登陆用户信息
        $adminId = $this->adminId;
        $adminService->updateUserInfoById($adminId, $password, $newPassword, $img);
        $this->ajaxReturn("");
    }

//    public function uptataImgAction(){
//        $img = $_POST['img'];
//        $username = $_POST['username'];
//        $adminService = service("Admin/AdminUserService");
//        $adminService->updateImg($username,$img);
//        $this->ajaxReturn("");
//
//    }
}
